package com.ocom.aiot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.ocom.aiot.dao.IDaoAppVerInfo;
import com.ocom.aiot.service.AppVerInfoService;
import com.ocom.aiot.vo.consumedevice.AddDeviceVerInfoVo;
import com.ocom.aiot.vo.consumedevice.GetDeviceVerInfoVo;
import com.ocom.common.entity.aiot.EntityXfAppVerInfo;
import com.ocom.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppVerInfoServiceImpl extends ServiceImpl<IDaoAppVerInfo, EntityXfAppVerInfo> implements AppVerInfoService {

    @Autowired
    private IDaoAppVerInfo appVerInfo;

    @Override
    public String selectAppVerUrl(String appVer) {
        QueryWrapper<EntityXfAppVerInfo> query = Wrappers.query();
        query.eq("app_ver",appVer);
        EntityXfAppVerInfo entityXfAppVerInfo = appVerInfo.selectOne(query);
        if (entityXfAppVerInfo == null){
            return  "";
        }else {
            return entityXfAppVerInfo.getApkFileUrl();
        }
    }


    @Override
    public EntityXfAppVerInfo selectAppVerInfo(String appVer) {
        QueryWrapper<EntityXfAppVerInfo> query = Wrappers.query();
        query.eq("app_ver",appVer);
        query.last("limit 1");
        return this.getOne(query);
    }

    //2.4.1.保存设备升级版本信息（addDeviceVerInfo）
    @Override
    public void AddDeviceVerInfo(AddDeviceVerInfoVo addDeviceVerInfoVo) {
        EntityXfAppVerInfo entityXfAppVerInfo = new EntityXfAppVerInfo();
        if (StringUtils.isNotEmpty(addDeviceVerInfoVo.getAppVer())){
            entityXfAppVerInfo.setAppVer(addDeviceVerInfoVo.getAppVer());
        }
        if (StringUtils.isNotEmpty(addDeviceVerInfoVo.getOldAppVer())){
            entityXfAppVerInfo.setOldAppVer(addDeviceVerInfoVo.getOldAppVer());
        }

        if (StringUtils.isNotEmpty(addDeviceVerInfoVo.getApkFileUrl())){
            entityXfAppVerInfo.setApkFileUrl(addDeviceVerInfoVo.getApkFileUrl());
        }

        if (StringUtils.isNotNull(addDeviceVerInfoVo.getStatus())){
            entityXfAppVerInfo.setStatus(addDeviceVerInfoVo.getStatus());
        }
        if (StringUtils.isNotNull(addDeviceVerInfoVo.getApkType())){
            entityXfAppVerInfo.setApkType(addDeviceVerInfoVo.getApkType());
        }
        appVerInfo.insert(entityXfAppVerInfo);
    }
    //2.4.2.删除设备升级版本（deleteDeviceVerInfo）
    @Override
    public void DeleteDeviceVerInfo(Integer id) {
        appVerInfo.deleteById(id);
    }

    // 2.4.2.获取设备升级版本列表（getDeviceVerInfo）
    @Override
    public List<EntityXfAppVerInfo> GetDeviceVerInfo(GetDeviceVerInfoVo getDeviceVerInfoVo) {
        QueryWrapper<EntityXfAppVerInfo> query = Wrappers.query();
        if (StringUtils.isNotEmpty(getDeviceVerInfoVo.getAppVer())){
            query.eq("app_ver",getDeviceVerInfoVo.getAppVer());
        }
        if (StringUtils.isNotEmpty(getDeviceVerInfoVo.getOldAppVer())){
            query.eq("old_app_ver",getDeviceVerInfoVo.getOldAppVer());
        }

        if (StringUtils.isNotNull(getDeviceVerInfoVo.getId())){
            query.eq("id",getDeviceVerInfoVo.getId());
        }

        if (StringUtils.isNotNull(getDeviceVerInfoVo.getStatus())){
            query.eq("status",getDeviceVerInfoVo.getStatus());
        }
        if (StringUtils.isNotNull(getDeviceVerInfoVo.getApkType())){
            query.eq("apk_type",getDeviceVerInfoVo.getApkType());
        }
        query.orderByDesc("create_time");
        PageHelper.startPage(getDeviceVerInfoVo.getPage(), getDeviceVerInfoVo.getSize());
        return this.list(query);
    }
}
